package us.gaaoc.framework.logic.validation;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.faces.application.FacesMessage;
import javax.faces.component.EditableValueHolder;

public class ConfirmPasswordValidator implements Validator {

	
	private String newPassword;
	  private String confirmPassword;
	  private boolean input1Set;
	
	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public boolean isInput1Set() {
		return input1Set;
	}

	public void setInput1Set(boolean input1Set) {
		this.input1Set = input1Set;
	}

	@Override
	public void validate(FacesContext context, UIComponent component,
		      Object value)
			throws ValidatorException {
		// TODO Auto-generated method stub

	    if (input1Set) {
	    	confirmPassword = (String) value;
	      if (!newPassword.equals(confirmPassword))
	      {
	        ((EditableValueHolder) component).setValid(false);
	        context.addMessage(component.getClientId(context), new FacesMessage(
	            "Passwords  must be Identical"));
	       
	        
	      }
	    } else {
	      input1Set = true;
	      newPassword = (String) value;
	    }
	  
	}

}
